package net.bwie.realtime.gongdan.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.gongdan.bean.*;
import net.bwie.realtime.gongdan.service.GongDanServince;
import net.bwie.realtime.gongdan.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: FuHe
 * @Date: 2025/6/13
 */
@RestController
@RequestMapping("/gongdan")
public class GongDanController {
    @Autowired
    GongDanServince gongDanServince;
    private static final String DATE_DEFAULT_VALUE = "1970-07-01";

    // todo 各个性别数量
    @RequestMapping("/gender/count")
    public String getBabyGenderCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarOne> vehicleBabyGenderReportList = gongDanServince.getBabyGenderCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleBabyGenderReportList == null || vehicleBabyGenderReportList.isEmpty()) {
            return "[]";
        }
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data", vehicleBabyGenderReportList);
        // 构建json字符串
        return JSON.toJSONString(resMap);
    }

    // todo 年龄段年龄段数量
    @RequestMapping("/age/count")
    public String getBabyAgeCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarTwo> vehicleBabyAgeReportList = gongDanServince.getBabyAgeCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleBabyAgeReportList == null || vehicleBabyAgeReportList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleBabyAgeReportList.stream().map(SugarTwo::getName).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleBabyAgeReportList.stream().map(SugarTwo::getData).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " +
                JSON.toJSONString(Camera_id)
                + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"数量\",\n" +
                "        \"data\": " +
                JSON.toJSONString(License_plate_count)
                + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    // todo 各省份年龄段数量
    @RequestMapping("/Province/count")
    public String getProvinceAgeCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarOne> vehicleProvinceAgeReportList = gongDanServince.getProvinceAgeCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleProvinceAgeReportList == null || vehicleProvinceAgeReportList.isEmpty()) {
            return "[]";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("mapData", vehicleProvinceAgeReportList);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data", map);
        // 构建json字符串
        return JSON.toJSONString(resMap);
    }

    /**
     * todo 性别数量
     */
    @RequestMapping("/gender2/count")
    public String getBabyGender2Count(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarOne> vehicleBabyGenderReportList = gongDanServince.getBabyGenderCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleBabyGenderReportList == null || vehicleBabyGenderReportList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleBabyGenderReportList.stream().map(SugarOne::getName).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleBabyGenderReportList.stream().map(SugarOne::getValue).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": " +
                JSON.toJSONString(Camera_id)
                + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"速度\",\n" +
                "        \"data\": " +
                JSON.toJSONString(License_plate_count)
                + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }


    /**
     * todo 每个性别人数
     */
    @RequestMapping("/genders/count")
    public String getBabyAgeGenderCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarThree> vehicleBabyAgeGenderReportList = gongDanServince.getBabyAgeGenderCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleBabyAgeGenderReportList == null || vehicleBabyAgeGenderReportList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getName).collect(Collectors.toList());
        List<Double> Camera_ids = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getNames).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getData).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                JSON.toJSONString(Camera_id)
                + "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"男\",\n" +
                "        \"data\": \n" +
                JSON.toJSONString(Camera_ids)
                + "        \n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"女\",\n" +
                "        \"data\": \n" +
                JSON.toJSONString(License_plate_count)
                + "        \n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}\n";
    }

    /**
     * todo 每个性别人数
     */
    @RequestMapping("/ageAndGender/count")
    public String getBabyAgeGendersCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarThree> vehicleBabyAgeGenderReportList = gongDanServince.getBabyAgeGenderCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleBabyAgeGenderReportList == null || vehicleBabyAgeGenderReportList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getName).collect(Collectors.toList());
        List<Double> Camera_ids = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getNames).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleBabyAgeGenderReportList.stream().map(SugarThree::getData).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": \n" +
                JSON.toJSONString(Camera_id)
                + "    ,\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"男\",\n" +
                "        \"data\": \n" +
                JSON.toJSONString(Camera_ids)
                + "        \n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"女\",\n" +
                "        \"data\": \n" +
                JSON.toJSONString(License_plate_count)
                + "        \n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}\n";
    }

    /**
     * todo 每个省份 男/女 性别人数 以及 未识别人数
     */
    @RequestMapping("/province/gender")
    public String getProvinceGendersCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<SugarFive> vehicleProvinceGenderReportList = gongDanServince.getProvinceGendersCount(date);
        // 如果没有数据，直接返回空字符串
        if (vehicleProvinceGenderReportList == null || vehicleProvinceGenderReportList.isEmpty()) {
            return "[]";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("mapData", vehicleProvinceGenderReportList);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data", map);
        return JSON.toJSONString(resMap);
    }
    /**
     *         Map<String, Object> map = new HashMap<>();
     *         map.put("mapData", vehicleProvinceAgeReportList);
     *         Map<String, Object> resMap = new HashMap<>();
     *         resMap.put("status", 0);
     *         resMap.put("data", map);
     */
    /**
     *         List<String> Camera_id = vehicleBabyAgeGenderReportList.stream().map(SugarFour::getName).collect(Collectors.toList());
     *         List<Double> Camera_ids = vehicleBabyAgeGenderReportList.stream().map(SugarFour::getNames).collect(Collectors.toList());
     *         List<Double> License_plate_count = vehicleBabyAgeGenderReportList.stream().map(SugarFour::getData).collect(Collectors.toList());
     *         List<Double> License_plate_count1 = vehicleBabyAgeGenderReportList.stream().map(SugarFour::getData1).collect(Collectors.toList());
     *                 "{\n" +
     *                 "  \"status\": 0,\n" +
     *                 "  \"data\": {\n" +
     *                 "    \"mapData\": [\n" +
     *                 "      {\n" +
     *                 "        \"name\": \n" +
     *                 JSON.toJSONString(Camera_id) +
     *                 "        ,\n" +
     *                 "        \"value\": " +
     *                 JSON.toJSONString(Camera_ids) +
     *                 ",\n" +
     *                 "        \"tooltipValues\": \n" +
     *                 JSON.toJSONString(License_plate_count)+
     *                 "        \n" +
     *                 "      }\n" +
     *                 "    ],\n" +
     *                 "    \"valueName\": \"男\",\n" +
     *                 "    \"tooltipNames\": [\n" +
     *                 "    \"女\",\n" +
     *                 "    \"未识别\"],\n" +
     *                 "    \"tooltipUnits\": [\n" +
     *                 "      \"人\",\n" +
     *                 "      \"人\"\n" +
     *                 "    ]\n" +
     *                 "  }\n" +
     *                 "}";
     */
}
